package com.miaostars.apakey.checks;

import com.miaostars.apakey.Apakey;
import com.miaostars.apakey.utils.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

public class MovementChecks {
    
    private final Apakey plugin;
    
    public MovementChecks(Apakey plugin) {
        this.plugin = plugin;
    }
    
    public boolean checkFly(Player player, PlayerData data) {
        if (!plugin.getConfigManager().getConfig().getBoolean("checks.fly.enabled")) {
            return false;
        }
        
        if (player.hasPermission("apakey.bypass")) {
            return false;
        }
        
        if (player.isFlying() || player.getAllowFlight()) {
            return false;
        }
        
        if (player.getVehicle() != null) {
            return false;
        }
        
        // Check if player is in liquid or climbing
        if (player.isInWater() || player.isInLava() || player.isClimbing()) {
            data.resetFlyViolations();
            return false;
        }
        
        // Check if player is on ground or has just jumped
        if (player.isOnGround() || System.currentTimeMillis() - data.getLastGroundTime() < 1000) {
            data.resetFlyViolations();
            return false;
        }
        
        // Check vertical movement
        Vector velocity = player.getVelocity();
        double sensitivity = plugin.getConfigManager().getConfig().getDouble("checks.fly.sensitivity");
        
        if (velocity.getY() >= -0.078 && velocity.getY() <= 0.078) {
            data.incrementFlyViolations();
            
            if (data.getFlyViolations() > plugin.getConfigManager().getConfig().getInt("checks.fly.max-violations")) {
                return true;
            }
        } else {
            data.decrementFlyViolations();
        }
        
        return false;
    }
    
    public boolean checkNoFall(Player player, PlayerData data) {
        if (!plugin.getConfigManager().getConfig().getBoolean("checks.nofall.enabled")) {
            return false;
        }
        
        if (player.hasPermission("apakey.bypass")) {
            return false;
        }
        
        if (player.isOnGround() || player.isFlying() || player.getAllowFlight()) {
            return false;
        }
        
        if (player.getVehicle() != null) {
            return false;
        }
        
        // Check if player is in liquid
        if (player.isInWater() || player.isInLava()) {
            data.resetNoFallViolations();
            return false;
        }
        
        // Check fall distance and velocity
        double sensitivity = plugin.getConfigManager().getConfig().getDouble("checks.nofall.sensitivity");
        double fallDistance = player.getFallDistance();
        Vector velocity = player.getVelocity();
        
        if (fallDistance > 3 && velocity.getY() >= -0.1) {
            data.incrementNoFallViolations();
            
            if (data.getNoFallViolations() > plugin.getConfigManager().getConfig().getInt("checks.nofall.max-violations")) {
                return true;
            }
        } else {
            data.decrementNoFallViolations();
        }
        
        return false;
    }
    
    public boolean checkHighJump(Player player, PlayerData data) {
        if (!plugin.getConfigManager().getConfig().getBoolean("checks.highjump.enabled")) {
            return false;
        }
        
        if (player.hasPermission("apakey.bypass")) {
            return false;
        }
        
        if (!player.isOnGround()) {
            return false;
        }
        
        double maxJumpHeight = plugin.getConfigManager().getConfig().getDouble("checks.highjump.max-jump-height");
        double jumpVelocity = player.getVelocity().getY();
        
        if (jumpVelocity > maxJumpHeight) {
            data.incrementHighJumpViolations();
            
            if (data.getHighJumpViolations() > plugin.getConfigManager().getConfig().getInt("checks.highjump.max-violations")) {
                return true;
            }
        } else {
            data.decrementHighJumpViolations();
        }
        
        return false;
    }
}